<?php
/*
# ------------------------------------------------------------------------
# JA Job Board Package
# ------------------------------------------------------------------------
# Copyright (C) 2004-2010 JoomlArt.com. All Rights Reserved.
# @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
# Author: JoomlArt.com
# Websites: http://www.joomlart.com - http://www.joomlancers.com.
# ------------------------------------------------------------------------
*/


jimport( 'joomla.application.component.model' );
JTable::addIncludePath(JPATH_COMPONENT_SITE.DS.'tables'); 

/**
 * @package Joomla
 * @subpackage JobBoard
 */
class JobBoardModelJANotes extends JModel
{
    var $_pagination = null;
    var $_table = null;
	var $_task_table= null;
	var $task_id = null;    
    
    function __construct($jbconfig = array()){
        parent::__construct($jbconfig);
        
        $this->set('name', 'notes');
        $this->set('fullname', JBPREFIX.'notes');
    }
    
    
//  PhuongLH - Save jobs - End -
     
    function remove($task = 'remove'){
        $cid = JRequest::getVar('cid',array(), 'post', 'array');
        $n =  count($cid);
        JArrayHelper::toInteger( $cid );
        $user = &JFactory::getUser();
		
        if($n && $user->id){
            if($task == 'remove'){
                $query = "DELETE FROM #__ja_notes WHERE user_id = ". (int)$user->id ." AND item_id = ". implode(" OR item_id = ", $cid);
                echo $query;
                $db = &JFactory::getDBO();
                $db->setQuery($query);
                if($db->query()){
                    return $n;
                }
            }
        }
        
        return 0;
    }
    
    function starred($task = 'starred'){
        global $mainframe;
        
        $cid = JRequest::getVar('cid');
        $cid = $cid[0];
        $is_starred = $task == 'starred' ? 1 : 0;
        $db = &JFactory::getDBO();
		
        // Check
        $user = &JFactory::getUser();
        $table_name = JRequest::getVar('table_name', 'ja_jobs');

        // Select id that already in the table
        if($cid){
            if($is_starred){
            	
            	$sql = "select id from #__ja_notes where user_id=".(int)$user->id." and item_id=$cid and listtype='$table_name'";
            	$db->setQuery($sql);
            	
            	$short_id = $db->loadResult();
            	
				$notestb = &$this->getTable();               
				// Insert
				if($short_id)	
					return 'done';
				$notestb->id = $short_id;
				$notestb->item_id = $cid;
				$notestb->user_id = $user->id;
				$notestb->listtype = $table_name;
				$notestb->save_date = date("Y-m-d H:i:s");
				
				$result =  $notestb->store();
				if ($result)
				return 'new';

			} else {
			    $query = "DELETE FROM #__ja_notes "
					." WHERE user_id = ". (int)$user->id 
					." AND listtype='$table_name' AND item_id = ". (int)$cid ;
			}
			$db->setQuery($query);
            return $db->query();
		}
        return false;
    }
    
    function addNote(){
        global $mainframe;
        
        $db = &JFactory::getDBO();
		
        // Check
        $user = &JFactory::getUser();
        $item = $this->getItemForAddNote();
		
        // Select id that already in the table
        $item->bind(JRequest::get('request'));
        
		$result = $item->store();				
		if ($result)
        	return $item->item_id.'_'.$item->description;
        else 	
        	return '';
    }
    /**
    * Get notes table instance
    * @return JobBoardTableJA_Notes Instance of notes table
    */
    function getTable(){
       if($this->_table == null){
           $this->_table = &JTable::getInstance('ja_notes', 'JobBoardTable');
       }
       return $this->_table;
    }
     
     function getItemForAddNote()
     {
     	$user = JFactory::getUser();
     	$db = JFactory::getDBO();
     	$item_id = JRequest::getVar('item_id');
     	$note_type = JRequest::getVar('note_type');
     	$query = "SELECT * FROM #__ja_notes WHERE item_id=".(int)$item_id." and user_id=".(int)$user->id." and note_type='$note_type'";
     	$db->setQuery($query);
     	$item = $db->loadObject();
     	if (!$item)
     		$item = $this->getTable();
     	else 
     	{
     		$table = $this->getTable();
     		$table->load($item->id);
     		$item = $table;
     	}
     	return $item;
     }
}
?>
